package com.tgy.cmp;

import com.tgy.Sort;

/**
 * @Author: tgy
 * @Date: 2020-10-22 15:14
 */
public class QuickSort02<E> extends Sort<E> {

    @Override
    public void sort() {

        sort(0,elements.length - 1);
    }

    public void sort(int begin, int end) {

        E base = elements[begin];

        int left = begin, right = end;

        while (left <= right) {

            while (compare(base,elements[right]) < 0) {

                right--;
            }

            while (compare(base, elements[left]) > 0) {

                left++;
            }

            if (left <= right) {

                swap(left,right);
                left++;
                right--;
            }
        }

        if (begin < right) {

            sort(begin,right);
        }

        if (left < end) {

            sort(left,end);
        }

    }
}
